package com.fjb.controller.product;


import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.entity.HttpCode;
import com.fjb.common.entity.JsonPagesResult;
import com.fjb.common.entity.JsonResult;
import com.fjb.common.entity.PageQuery;
import com.fjb.common.entity.TotalResult;
import com.fjb.common.enums.DataSource;
import com.fjb.common.enums.brand.BrandStatus;
import com.fjb.common.enums.product.AttrType;
import com.fjb.common.enums.product.CategoryStatus;
import com.fjb.common.enums.product.ProductStatus;
import com.fjb.common.enums.product.ProductType;
import com.fjb.common.enums.recycle.RecycleStatus;
import com.fjb.common.enums.user.CollectType;
import com.fjb.common.global.GlobalConfig;
import com.fjb.common.utils.SerialNumberUtils;
import com.fjb.controller.base.BaseController;
import com.fjb.dto.LoginUserInfo;
import com.fjb.pojo.brand.BrandInfo;
import com.fjb.pojo.brand.vo.BrandInfoVo;
import com.fjb.pojo.product.ProductAttribute;
import com.fjb.pojo.product.ProductAttributeSku;
import com.fjb.pojo.product.ProductInfo;
import com.fjb.pojo.product.query.ProductCategoryQuery;
import com.fjb.pojo.product.query.ProductInfoQuery;
import com.fjb.pojo.product.vo.ProductCategoryVo;
import com.fjb.pojo.product.vo.ProductFilesVo;
import com.fjb.pojo.product.vo.ProductInfoPageVo;
import com.fjb.pojo.product.vo.ProductSpecAttributeVo;
import com.fjb.pojo.product.vo.ProductSpecificationVo;
import com.fjb.pojo.user.SysUserCollect;
import com.fjb.pojo.user.SysUserPraise;
import com.fjb.service.brand.BrandInfoService;
import com.fjb.service.product.ProductAttributeService;
import com.fjb.service.product.ProductAttributeSkuService;
import com.fjb.service.product.ProductCategoryService;
import com.fjb.service.product.ProductFilesService;
import com.fjb.service.product.ProductInfoService;
import com.fjb.service.user.SysUserCollectService;
import com.fjb.service.user.SysUserPraiseService;

/**
 * <p>
 * 产品信息 前端控制器
 * </p>
 *
 * @author hemiao
 * @since 2019-12-22
 */
@Controller
@RequestMapping("/product")
public class ProductInfoController extends BaseController{
	
	@Autowired
	private ProductInfoService productInfoService;
	@Autowired
	private ProductAttributeService productAttributeService;
	@Autowired
	private ProductAttributeSkuService productAttributeSkuService;
	@Autowired
	private SysUserCollectService sysUserCollectService;
	@Autowired
	private SysUserPraiseService sysUserPraiseService;
	@Autowired
	private BrandInfoService brandInfoService;
	@Autowired
	private ProductCategoryService productCategoryService;
	
	/**
	 * @Description:获得产品详情
	 * @param request
	 * @param productId
	 * @return
	 * JsonResult<Map<String,Object>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年1月31日 下午10:53:59
	 */
	@RequestMapping(value="/selectDetails")
	@ResponseBody
	public JsonResult<Map<String, Object>> selectList(HttpServletRequest request,Integer productId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(productId==null) {
			return new JsonResult<Map<String,Object>>(null, httpCode);
		}
		HashedMap<String, Object> resultMap = new HashedMap<String, Object>();
		// 产品信息
		ProductInfoPageVo productInfo = productInfoService.selectGetOneById(productId);
		if(productInfo==null) {
			httpCode.setMsg("产品信息不存在");
			return new JsonResult<Map<String,Object>>(null, httpCode);
		}
		Integer productStatus = productInfo.getProductStatus();
		if(!productStatus.equals(ProductStatus.STATUS_1.getCode())) {
			httpCode.setMsg("产品未上架");
			return new JsonResult<Map<String,Object>>(null, httpCode);
		}	
		String mainFileUrl = productInfo.getMainFileUrl();
		resultMap.put("productInfo", productInfo);
		String productFiles = productInfo.getProductFiles();
			
		// 文件列表
		if(StringUtils.isNoneBlank(mainFileUrl)) {
			// 主文件显示			
			productInfo.setMainFileUrl(GlobalConfig.FILE_SERVICE_LOCATION+mainFileUrl);
		}
		
		List<String> fileList = new ArrayList<String>();
		if(StringUtils.isNotBlank(productFiles)) {
			String[] productFilesStr = productFiles.split(",");
			for (String filesStr : productFilesStr) {
				fileList.add(GlobalConfig.FILE_SERVICE_LOCATION+filesStr);
			}
		}
		resultMap.put("fileList", fileList);
		
		// 获得属性列表
		QueryWrapper<ProductAttribute> queryWrapper = new QueryWrapper<ProductAttribute>();
		queryWrapper.eq("product_id", productId);
		List<ProductAttribute> allAttributeList = productAttributeService.list(queryWrapper);
		
		// 获得属性 sku列表
		QueryWrapper<ProductAttributeSku> queryWrapperSku = new QueryWrapper<ProductAttributeSku>();
		queryWrapperSku.eq("product_id", productId);
		List<ProductAttributeSku> skuList = productAttributeSkuService.list(queryWrapperSku);
		
		List<ProductSpecificationVo> specificationList = new ArrayList<ProductSpecificationVo>();
		List<ProductSpecAttributeVo> attrList = new ArrayList<ProductSpecAttributeVo>();
		
		for (ProductAttribute attr : allAttributeList) {
			Integer attrType = attr.getAttrType();
			Integer attrId = attr.getId();
			String attrName = attr.getAttrName();
			if(AttrType.TYPE_1.getCode().equals(attrType)) {
				String titleCode = attr.getAttrTitleCode();
				Integer attrLevel = attr.getAttrLevel();
				ProductSpecificationVo spec = new ProductSpecificationVo();
				spec.setAttrId(attrId);
				spec.setAttrTitle(attrName);
				spec.setTitleCode(titleCode);
				spec.setAttrLevel(attrLevel);
				specificationList.add(spec);
			}else if(AttrType.TYPE_2.getCode().equals(attrType)){
				String titleCode = attr.getAttrTitleCode();
				String attrCodeOne = attr.getAttrCodeOne();
				String attrCodeTwo = attr.getAttrCodeTwo();
				ProductSpecAttributeVo specAttr = new ProductSpecAttributeVo();
				specAttr.setAttrId(attrId);	
				specAttr.setAttrCodeOne(attrCodeOne);
				specAttr.setAttrCodeTwo(attrCodeTwo);
				specAttr.setTitleCode(titleCode);
				specAttr.setAttrName(attrName);
				attrList.add(specAttr);
			}
		}	
		
		if(specificationList.size() > 0 && attrList.size()>0) {
			BigDecimal b0 = new BigDecimal(0);
			for (ProductSpecificationVo spec : specificationList) {
				String titleCode = spec.getTitleCode();
				Integer attrLevel = spec.getAttrLevel();
				
				List<ProductSpecAttributeVo> attributeList = new ArrayList<ProductSpecAttributeVo>();
				
				List<ProductSpecAttributeVo> temporaryAttrList = new ArrayList<ProductSpecAttributeVo>(attrList);;
				for (ProductSpecAttributeVo attributeVo : temporaryAttrList) {
					String titleCode2 = attributeVo.getTitleCode();
					if(titleCode.equals(titleCode2)) {	
						String attrCodeOne = attributeVo.getAttrCodeOne();
						String attrCodeTwo = attributeVo.getAttrCodeTwo();
						String attrFileUrl = attributeVo.getAttrFileUrl();
						if(StringUtils.isNoneBlank(attrFileUrl)) {
							attributeVo.setAttrFileUrl(GlobalConfig.FILE_SERVICE_LOCATION+attrFileUrl);
						}	
						
						ProductAttributeSku sku = getBaseProductAttributeSku(titleCode, attrCodeOne, attrCodeTwo, skuList,attrLevel);
						if(sku!=null) {	
							attributeVo.setCheckedSku("selected");
							// sku 库存
							Integer skuCount = sku.getSkuCount();
							// sku 价格
							BigDecimal skuPrice = sku.getSkuPrice();
							if(skuCount!=null) {	
								attributeVo.setSkuCount(skuCount);
							}else {
								attributeVo.setSkuCount(0);
							}
							if(skuPrice!=null) {
								attributeVo.setSkuPrice(skuPrice);
							}else {
								attributeVo.setSkuPrice(b0);
							}		
							Integer attrSkuId = sku.getId();
							attributeVo.setAttrSkuId(attrSkuId);
						}else {
							attributeVo.setSkuCount(0);
							attributeVo.setSkuPrice(b0);
							attributeVo.setCheckedSku("false");
						}	
						attributeVo.setAttrLevel(attrLevel);
						attributeList.add(attributeVo);
					}
				}	
				spec.setAttributeList(attributeList);
			}
		}
		// 规格列表		specificationList
		resultMap.put("specificationList", specificationList);
		
		// 评论列表  evaluateList	
		List<ProductAttributeSku> evaluateList = new ArrayList<ProductAttributeSku>();
		resultMap.put("evaluateList", new TotalResult<List<ProductAttributeSku>>(evaluateList, Long.valueOf(0), 0));
		
		// 该商品是否收藏	
		Integer userHasCollect = 0;
		// 是否超赞
		Integer userPraise = 0;
		LoginUserInfo userInfo = getBaseWxTokenIdTwo(request);
		if(userInfo!=null) {
			Integer userId = userInfo.getUserId();
			QueryWrapper<SysUserCollect> userCollectWrapper = new QueryWrapper<SysUserCollect>();
			userCollectWrapper.eq("user_id", userId);
			userCollectWrapper.eq("collect_type", CollectType.TYPE_1.getCode());
			userCollectWrapper.eq("business_id", productId);
			SysUserCollect userCollect = sysUserCollectService.getOne(userCollectWrapper);
			if(userCollect!=null) {
				userHasCollect = 1;
			}
			QueryWrapper<SysUserPraise> userPraiseWrapper = new QueryWrapper<SysUserPraise>();
			userPraiseWrapper.eq("user_id", userId);
			userPraiseWrapper.eq("product_id", productId);
			SysUserPraise sysUserPraise = sysUserPraiseService.getOne(userPraiseWrapper);
			if(sysUserPraise!=null) {
				userPraise = 1;
			}	
		}
		resultMap.put("userHasCollect", userHasCollect);
		resultMap.put("userPraise", userPraise);
		
		// 增加一次浏览次数
		productInfoService.updateViewCountAdd(productId);
		return new JsonResult<Map<String,Object>>(resultMap, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:产品列表查询
	 * @param request
	 * @param pageQuery
	 * @param categoryId
	 * @param brandId
	 * @return
	 * JsonPagesResult<List<ProductInfoPageVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月11日 下午11:05:58
	 */
	@RequestMapping(value = "/selectPageList")
    @ResponseBody
    public JsonPagesResult<List<ProductInfoPageVo>> selectPagesList(HttpServletRequest request,
    		PageQuery pageQuery,Integer categoryId ,Integer brandId){
		ProductInfoQuery productQuery = new ProductInfoQuery();
		if(categoryId!=null) {			
			productQuery.setCategoryId(categoryId);
		}
		if(brandId!=null) {			
			productQuery.setBrandId(brandId);
		}
        return productInfoService.selectPagesList(pageQuery,productQuery);
    }
	
	/**
	 * @Description:添加产品信息  
	 * @param request
	 * @param dealType			交易类型  1、出售     2、回收(收购）
	 * @param originalPrice		原价
	 * @param currentPrice		实际交易价格
	 * @param productCount		产品数量
	 * @param brandId			品牌id
	 * @param categoryId		车型 分类id
	 * @return
	 * JsonResult<Integer>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月16日 下午4:18:17
	 */
	@RequestMapping(value="/addInfo")
	@ResponseBody
	public JsonResult<Integer> addInfo(HttpServletRequest request,Integer dealType ,BigDecimal originalPrice,
			BigDecimal currentPrice, Integer productCount,Integer brandId ,Integer categoryId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(dealType==null) {
			return new JsonResult<Integer>(null, httpCode);
		}
		if(brandId==null) {
			httpCode.setMsg("品牌不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(categoryId==null) {
			httpCode.setMsg("车型不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(currentPrice==null) {
			httpCode.setMsg("实际价格不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(productCount==null) {
			httpCode.setMsg("数量不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productName = request.getParameter("productName");
		if(StringUtils.isBlank(productName)) {
			httpCode.setMsg("名称不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}	
		String productUnit = request.getParameter("productUnit");
		if(StringUtils.isBlank(productUnit)) {
			httpCode.setMsg("数量单位不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productFiles = request.getParameter("productFiles");
		if(StringUtils.isBlank(productFiles)) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productDetails = request.getParameter("productDetails");
		if(StringUtils.isBlank(productDetails)) {
			httpCode.setMsg("内容不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String attrName = request.getParameter("attrName");
		if(StringUtils.isBlank(attrName)) {
			httpCode.setMsg("属性名称不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productTitle = request.getParameter("productTitle");
		if(StringUtils.isBlank(productTitle)) {
			productTitle = "";
		}
		String[] filesSplit = productFiles.split(",");
		String mainFileUrl = "";
		for (String fileStr : filesSplit) {
			mainFileUrl = fileStr;
			break;
		}
		
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		Integer mainUserId = userInfo.getMainUserId();
		
		String productNumber = SerialNumberUtils.createNumber(SerialNumberUtils.PRODUCT_INFO, mainUserId);
		BigDecimal b0 = new BigDecimal(0);
		ProductInfo pi = new ProductInfo();
		pi.setMainUserId(mainUserId);
		pi.setUserId(userId);
		pi.setBrandId(brandId);	
		pi.setCategoryId(categoryId);
		pi.setDealType(dealType);
		pi.setProductType(0);	
		pi.setProductNumber(productNumber);
		pi.setProductUnit(productUnit);
		pi.setProductName(productName);	
		pi.setProductTitle(productTitle);
		pi.setMainFileUrl(mainFileUrl);
		pi.setProductFiles(productFiles);
		pi.setProductCount(productCount);
		pi.setCurrentPrice(currentPrice);
		if(originalPrice==null) {
			originalPrice = b0;
		}	
		pi.setOriginalPrice(originalPrice);
		pi.setCreateUserId(userId);
		pi.setCreateTime(LocalDateTime.now());
		pi.setProductStatus(ProductStatus.STATUS_1.getCode());
		pi.setDataSource(DataSource.WX_APPLET.getCode());
		pi.setIsNew(0);
		pi.setIsHot(0);
		pi.setIsOnSale(0);
		pi.setViewCount(0);
		pi.setWantCount(0);	
		pi.setPraiseCount(0);	
		pi.setProductDetails(productDetails);
		return productInfoService.addInfo(pi, attrName);
	}
	
	/**
	 * @Description:查询我的产品列表
	 * @param request
	 * @param pageQuery
	 * @param dealType
	 * @return
	 * JsonPagesResult<List<ProductInfoPageVo>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月19日 下午9:29:34
	 */
	@RequestMapping(value = "/selectMyPagesList")
    @ResponseBody
    public JsonPagesResult<List<ProductInfoPageVo>> selectMyPagesList(HttpServletRequest request,
    		PageQuery pageQuery,Integer dealType){
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		ProductInfoQuery productQuery = new ProductInfoQuery();
		if(dealType!=null) {	
			if(dealType>0) {				
				productQuery.setDealType(dealType);
			}
		}	
		productQuery.setUserId(userId);
		return productInfoService.selectMyPagesList(pageQuery,productQuery);
    }
	
	/**
	 * @Description:必须登录才能查看 
	 * @param request
	 * @param productId
	 * @return
	 * JsonResult<String>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月24日 下午8:51:51
	 */
	@RequestMapping(value = "/getProductPhone")
    @ResponseBody
    public JsonResult<String> getProductPhone(HttpServletRequest request,Integer productId){
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		Integer mainUserId = userInfo.getMainUserId();
		return productInfoService.getProductPhone(mainUserId, userId, productId);
    }
	
	/**
	 * @Description:获得产品修改信息 
	 * @param request
	 * @param productId
	 * @return
	 * JsonResult<Map<String,Object>>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月29日 下午6:32:14
	 */
	@RequestMapping(value="/getUpdateInfo")
	@ResponseBody
	public JsonResult<Map<String, Object>> getUpdateInfo(HttpServletRequest request,Integer productId) {
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(productId==null) {
			return new JsonResult<Map<String, Object>>(null, httpCode);
		}
		HashMap<String, Object> resultMap = new HashMap<String,Object>();
		
		// 产品信息
		ProductInfo productInfo = productInfoService.getById(productId);
		if(productInfo==null) {
			httpCode.setMsg("产品不存在");
			return new JsonResult<Map<String,Object>>(null, httpCode);
		}
		
		// 品牌id
		resultMap.put("brandId", productInfo.getBrandId());
		// 分类id
		resultMap.put("categoryId", productInfo.getCategoryId());
		// 产品名称
		resultMap.put("productName", productInfo.getProductName());
		// 产品
		resultMap.put("productUnit", productInfo.getProductUnit());
		// 产品文件
		resultMap.put("productFiles", productInfo.getProductFiles());
		// 产品 价格
		resultMap.put("currentPrice", productInfo.getCurrentPrice());
		// 产品数量
		resultMap.put("productCount", productInfo.getProductCount());
		// 产品详情
		resultMap.put("productDetails", productInfo.getProductDetails());
		
		// 获得产品属性
		QueryWrapper<ProductAttribute> queryWrapper = new QueryWrapper<ProductAttribute>();
		queryWrapper.eq("product_id", productId);
		List<ProductAttribute> attrList = productAttributeService.list(queryWrapper);
		String attrTitle = "";
		String attrName = "";
		for (ProductAttribute attr : attrList) {
			Integer attrType = attr.getAttrType();
			if(AttrType.TYPE_1.getCode().equals(attrType)) {
				attrTitle = attr.getAttrName();
				continue;
			}
			if(AttrType.TYPE_2.getCode().equals(attrType)) {
				attrName = attr.getAttrName();
				continue;
			}
		}
		resultMap.put("attrTitle", attrTitle);
		resultMap.put("attrName", attrName);
			
		// 文件服务器前缀		
		resultMap.put(FILE_SERVICE_LOCATION, GlobalConfig.FILE_SERVICE_LOCATION);
		
		BrandInfo brandInfo = new BrandInfo();
		brandInfo.setBrandStatus(BrandStatus.STATUS_1.getCode());
		BrandInfoVo bi = new BrandInfoVo();
		bi.setId(0);
		bi.setBrandName("品牌");
		List<BrandInfoVo> allBrandList = new ArrayList<BrandInfoVo>();
		allBrandList.add(bi);
		List<BrandInfoVo> brandList = brandInfoService.selectBrandList(brandInfo);
		if(brandList.size()>0) {
			allBrandList.addAll(brandList);
			for (BrandInfoVo brandInfoVo : brandList) {
				String logo = brandInfoVo.getBrandLogo();
				if(StringUtils.isNoneBlank(logo)) {
					brandInfoVo.setBrandLogo(GlobalConfig.FILE_SERVICE_LOCATION+logo);
				}
			}
		}		
		resultMap.put("allBrandList", allBrandList);
		
		ProductCategoryQuery categoryQuery = new ProductCategoryQuery();
		categoryQuery.setCategoryStatus(CategoryStatus.STATUS_1.getCode());
		List<ProductCategoryVo> categoryoList = productCategoryService.selectList(categoryQuery);
		resultMap.put("allCategoryList", categoryoList);
		
		return new JsonResult<Map<String,Object>>(resultMap, HttpCode.SUCCESS);
	}
	
	/**
	 * @Description:产品修改
	 * @param request
	 * @param productId
	 * @param originalPrice
	 * @param currentPrice
	 * @param productCount
	 * @param brandId
	 * @param categoryId
	 * @return
	 * JsonResult<Integer>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月29日 下午5:24:47
	 */
	@RequestMapping(value="/updateInfo")
	@ResponseBody
	public JsonResult<Integer> updateInfo(HttpServletRequest request,Integer productId,BigDecimal originalPrice,
			BigDecimal currentPrice, Integer productCount,Integer brandId ,Integer categoryId){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(productId==null) {
			return new JsonResult<Integer>(null, httpCode);
		}	
		if(productId.equals(0)) {
			return new JsonResult<Integer>(null, httpCode);
		}
		if(brandId==null) {
			httpCode.setMsg("品牌不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(categoryId==null) {
			httpCode.setMsg("车型不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(currentPrice==null) {
			httpCode.setMsg("实际价格不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		if(productCount==null) {
			httpCode.setMsg("数量不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productName = request.getParameter("productName");
		if(StringUtils.isBlank(productName)) {
			httpCode.setMsg("名称不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}	
		String productUnit = request.getParameter("productUnit");
		if(StringUtils.isBlank(productUnit)) {
			httpCode.setMsg("数量单位不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productFiles = request.getParameter("productFiles");
		if(StringUtils.isBlank(productFiles)) {
			httpCode.setMsg("文件不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productDetails = request.getParameter("productDetails");
		if(StringUtils.isBlank(productDetails)) {
			httpCode.setMsg("内容不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String attrName = request.getParameter("attrName");
		if(StringUtils.isBlank(attrName)) {
			httpCode.setMsg("属性名称不能为空");
			return new JsonResult<Integer>(null, httpCode);
		}
		String productTitle = request.getParameter("productTitle");
		if(StringUtils.isBlank(productTitle)) {
			productTitle = "";
		}
		String[] filesSplit = productFiles.split(",");
		String mainFileUrl = "";
		for (String fileStr : filesSplit) {
			mainFileUrl = fileStr;
			break;
		}
		
		// 查询产品
		ProductInfo pi = productInfoService.getById(productId);
		if(pi==null) {
			httpCode.setMsg("产品信息不存在");
			return new JsonResult<Integer>(null, httpCode);
		}
		
		LoginUserInfo userInfo = getBaseWxTokenId(request);
		Integer userId = userInfo.getUserId();
		Integer userId2 = pi.getUserId();
		if(!userId.equals(userId2)) {
			return new JsonResult<Integer>(null, HttpCode.NO_OPERATION_PERMISSION);
		}
		BigDecimal b0 = new BigDecimal(0);
		pi.setUserId(userId);
		pi.setBrandId(brandId);	
		pi.setCategoryId(categoryId);
		pi.setProductType(0);	
		pi.setProductUnit(productUnit);
		pi.setProductName(productName);	
		pi.setProductTitle(productTitle);
		pi.setMainFileUrl(mainFileUrl);
		pi.setProductFiles(productFiles);
		pi.setProductCount(productCount);
		pi.setCurrentPrice(currentPrice);
		if(originalPrice==null) {
			originalPrice = b0;
		}	
		pi.setOriginalPrice(originalPrice);
		pi.setUpdateUserId(userId);
		pi.setUpdateTime(LocalDateTime.now());
		pi.setProductDetails(productDetails);
		return productInfoService.updateInfo(pi, attrName);
	}
	
	/**
	 * @Description:修改产品状态
	 * @param request
	 * @param productId
	 * @param updateStatus
	 * @return
	 * JsonResult<Integer>
	 * @exception:
	 * @author: hemiao
	 * @time:2020年2月29日 下午11:42:21
	 */
	@RequestMapping(value="/updateStatus")
	@ResponseBody
	public JsonResult<Integer> updateStatus(HttpServletRequest request,Integer productId,Integer updateStatus){
		HttpCode httpCode = HttpCode.PARAM_VERIFICATION;
		if(productId==null||updateStatus==null) {
			return new JsonResult<Integer>(null, httpCode);
		}
		ProductStatus productStatus2 = ProductStatus.STATUS_2;
		ProductStatus productStatus5 = ProductStatus.STATUS_5;
		ProductStatus productStatus1 = ProductStatus.STATUS_1;
		if(updateStatus.equals(productStatus2.getCode())||updateStatus.equals(productStatus5.getCode())
				||updateStatus.equals(productStatus1.getCode())) {
			LoginUserInfo userInfo = getBaseWxTokenId(request);
			Integer userId = userInfo.getUserId();
			if(productStatus1.getCode().equals(updateStatus)) {
				// 查询当前状态是否是系统下架
				ProductInfo oldPi = productInfoService.getById(productId);
				if(oldPi==null) {	
					httpCode.setMsg("产品信息不存在");
					return new JsonResult<Integer>(null, httpCode);
				}
				Integer userId2 = oldPi.getUserId();
				if(!userId.equals(userId2)) {
					return new JsonResult<Integer>(null, HttpCode.NO_OPERATION_PERMISSION);
				}
				Integer oldStatus = oldPi.getProductStatus();
				if(oldStatus.equals(ProductStatus.STATUS_3.getCode())) {
					httpCode.setMsg("系统下架,请联系管理员");
					return new JsonResult<Integer>(null, httpCode);
				}
			}
			QueryWrapper<ProductInfo> updateWrapper = new QueryWrapper<ProductInfo>();
			updateWrapper.eq("id", productId);
			updateWrapper.eq("user_id", userId);	
			ProductInfo updateInfo = new ProductInfo();
			updateInfo.setProductStatus(updateStatus);
			updateInfo.setUpdateTime(LocalDateTime.now());
			updateInfo.setUpdateUserId(userId);
			boolean b = productInfoService.update(updateInfo, updateWrapper);
			if(b==false) {
				httpCode.setMsg("状态变更失败,请联系管理员");
				return new JsonResult<Integer>(null, httpCode);
			}
		}else {
			httpCode.setMsg("产品状态不匹配");
			return new JsonResult<Integer>(null, httpCode);
		}
		return new JsonResult<Integer>(null, HttpCode.SUCCESS);
	}
}
